layui.define(function(exports){

  /* 下面通过 layui.use 分段加载不同的模块，实现不同区域的同时渲染，从而保证视图的快速呈现 */

  //区块轮播切换
  layui.use(['admin', 'carousel'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,carousel = layui.carousel
    ,element = layui.element
    ,device = layui.device();

    //轮播切换
    $('.layadmin-carousel').each(function(){
      var othis = $(this);
      carousel.render({
        elem: this
        ,width: '100%'
        ,arrow: 'none'
        ,interval: othis.data('interval')
        ,autoplay: othis.data('autoplay') === true
        ,trigger: (device.ios || device.android) ? 'click' : 'hover'
        ,anim: othis.data('anim')
      });
    });
    
    element.render('progress');
    
  });

  //数据概览
  layui.use(['admin', 'carousel', 'echarts'], function(){
    var $ = layui.$
        ,admin = layui.admin
        ,carousel = layui.carousel
        ,echarts = layui.echarts;

    var echartsApp = [],options =[]
        ,elemDataView = $('#LAY-index-dataview').children('div')
        ,renderDataView = function(index){
      if(options.length > index) {
        echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
        echartsApp[index].setOption(options[index]);
        //window.onresize = echartsApp[index].resize;
        admin.resize(function () {
          echartsApp[index].resize();
        });
      }
    };

    //没找到DOM，终止执行
    if(!elemDataView[0]) return;

    $.ajax({
      type: "post",
      url: layui.setter.webRoot + '/console/order/charts.data',
      dataType:"json",
      success:function(res){
        if(res.code == 200){
          options = res.body.optionList;
          renderDataView(0);
        }
      }
    });

    //触发数据概览轮播
    var carouselIndex = 0;
    carousel.on('change(LAY-index-dataview)', function(obj){
      renderDataView(carouselIndex = obj.index);
    });

    //触发侧边伸缩
    layui.admin.on('side', function(){
      setTimeout(function(){
        renderDataView(carouselIndex);
      }, 300);
    });

    //触发路由
    layui.admin.on('hash(tab)', function(){
      layui.router().path.join('') || renderDataView(carouselIndex);
    });
  });


  //地图
  layui.use(['carousel', 'echarts'], function(){
    var $ = layui.$
        ,carousel = layui.carousel
        ,echarts = layui.echarts;

    var echartsApp = [], options = []
        ,elemDataView = $('#LAY-index-pagethree-home').children('div')
        ,renderDataView = function(index){
      if(options.length > index) {
        echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
        echartsApp[index].setOption(options[index]);
        window.onresize = echartsApp[index].resize;
      }
    };
    //没找到DOM，终止执行
    if(!elemDataView[0]) return;

    $.ajax({
      type: "post",
      url: layui.setter.webRoot + '/console/map/charts.data',
      dataType:"json",
      success:function(res){
        if(res.code == 200){
          options = res.body.optionList;
          renderDataView(0);
        }
      }
    });
  });

  exports('console', {})
});